package net.techwatch.contacts.client;

import java.util.List;

import net.techwatch.contacts.shared.Contact;


import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class Controller {

	private static final ContactServiceAsync service = GWT.create(ContactService.class);
	
	private ContactsView view;
	
	public Controller(ContactsView view) {
		this.view = view;
	}
	
	public void getAllContacts() {
		service.getAllContacts(new AsyncCallback<List<Contact>> () {
		
			@Override
			public void onSuccess(List<Contact> result) {
				view.loadData(result);
			}
		
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
	}
	
	public void deleteContacts(List<Contact> contacts) {
		service.deleteContacts(contacts, new AsyncCallback<Void> () {
		
			@Override
			public void onSuccess(Void result) {
				getAllContacts();
			}
		
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
		
	}
	
	public void addContacts(Contact c) {
		service.addContacts(c, new AsyncCallback<Void>() {
		
			@Override
			public void onSuccess(Void result) {
				getAllContacts();
			}
		
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
	}
}
